﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using fsw.Common;

namespace fsw.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (DTRequest.IsPost())
            {
                Response.Write(DoExits());
                Response.End();
            }
        }
        #region =====验证登录=====
        /// <summary>
        /// 验证登录
        /// </summary>
        /// <returns></returns>
        private string DoExits()
        {
            Model.JsonError jsonerror = new Model.JsonError();
            Model.Admin_Log model_log = new Model.Admin_Log();
            BLL.Admin_Log bll_log = new BLL.Admin_Log();
            string ip = DTRequest.GetIP();
            jsonerror.state = "error";
            string UserName = DTRequest.GetFormString("t0").ToString();
            string PassWord = DTRequest.GetFormString("t1").ToString();
            string VerifyCode = DTRequest.GetFormString("t2").ToString();
            string VerifyCode_1 = Session[DTKeys.SESSION_CODE].ToString();
            model_log.AddDate =Utils.ConvertDateTimeInt( DateTime.Now);
            model_log.UserIP = ip;
            model_log.UserName = UserName;
            model_log.UserAgent = DTRequest.GetUserAgent();
            if (UserName.Equals("") || PassWord.Equals(""))
            {
                jsonerror.msg = "请输入用户名或密码！";
                model_log.Remark = jsonerror.msg;
                bll_log.Add(model_log);
                return JsonHelper.ObjectToJSON(jsonerror);
            }
            if (VerifyCode == "")
            {
                jsonerror.msg = "请输入验证码！";
                model_log.Remark = jsonerror.msg;
                bll_log.Add(model_log);
                return JsonHelper.ObjectToJSON(jsonerror);
            }
            if (VerifyCode.ToLower() != VerifyCode_1.ToLower())
            {
                jsonerror.msg = "验证码不正确！";
                model_log.Remark = jsonerror.msg;
                bll_log.Add(model_log);
                return JsonHelper.ObjectToJSON(jsonerror);
            }
            if (Session["AdminLoginSun"] == null)
            {
                Session["AdminLoginSun"] = 1;
            }
            else
            {
                Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
            }
            //判断登录错误次数
            if (Session["AdminLoginSun"] != null && Convert.ToInt32(Session["AdminLoginSun"]) > 5)
            {
                jsonerror.msg = "错误超过5次，关闭浏览器重新登录！";
                return JsonHelper.ObjectToJSON(jsonerror);
            }
            BLL.Admin bll = new BLL.Admin();
            Model.Admin model = bll.GetModel(UserName, PassWord, true);
            if (model == null)
            {
                jsonerror.msg = "用户名或密码有误，请重试！";
                model_log.Remark = jsonerror.msg;
                bll_log.Add(model_log);
                return JsonHelper.ObjectToJSON(jsonerror);
            }
            if (model.isStatus == 0)
            {
                jsonerror.msg = "该用户名已经锁定！";
                model_log.Remark = jsonerror.msg;
                bll_log.Add(model_log);
                return JsonHelper.ObjectToJSON(jsonerror);
            }
            model.Logins = model.Logins + 1;
            model.LastIP = ip;
            model.LastTime = Utils.ConvertDateTimeInt(DateTime.Now);
            bll.Update(model);
            Session[DTKeys.SESSION_ADMIN_INFO] = model;
            Session.Timeout = 45;
            jsonerror.state = "success";
            jsonerror.msg = "登录成功！";
            model_log.Remark = jsonerror.msg;
            model_log.UserId = model.Id;
            bll_log.Add(model_log);         
            Utils.WriteCookie("AdminName", "fsw", model.UserName);
            Utils.WriteCookie("AdminPwd", "fsw", model.PassWord);
            return JsonHelper.ObjectToJSON(jsonerror);
        }
        #endregion
    }
}